﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

//namespace CSE445_Assignment2_ChickenSales
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//        }
//    }
//}

namespace ConsoleAppPriceCut
{
    public delegate void priceCutEvent(Int32 pr);
    public class ChickenFarm
    {
        static Random rng = new Random();
        public static event priceCutEvent priceCut; // Define event
        private static Int32 chickenPrice = 10;
        public Int32 getPrice() { return chickenPrice; }
        public static void changePrice(Int32 price)
        {
            if (price < chickenPrice)
            { // a price cut 
                if (priceCut != null)  // there is at least a subscriber
                    priceCut(price); // emit event to subscribers
            }
            chickenPrice = price;
        }





        public void farmerFunc()
        {
            for (Int32 i = 0; i < 50; i++)
            {
                Thread.Sleep(500);
                // Take the order from the queue of the orders;
                // Decide the price based on the orders
                Int32 p = rng.Next(5, 10); // Generate a random price
                // Console.WriteLine("New Price is {0}", p);
                ChickenFarm.changePrice(p);
            }
        }



        public class Retailer
        {
            public void retailerFunc()
            {   //for starting thread
                ChickenFarm chicken = new ChickenFarm();
                for (Int32 i = 0; i < 10; i++)
                {
                    Thread.Sleep(500);
                    Int32 p = chicken.getPrice();
                    Console.WriteLine("Store{0} has everyday low price: ${1} each", Thread.CurrentThread.Name, p);
                }
            }
            public void chickenOnSale(Int32 p)
            {  // Event handler
                Console.WriteLine("Store{0} chickens are on sale: as low as ${1} each", Thread.CurrentThread.Name, p);
            }
        }


        public class myApplication
        {
            static void Main(string[] args)
            {
                ChickenFarm chicken = new ChickenFarm();
                Thread farmer = new Thread(new ThreadStart(chicken.farmerFunc));
                farmer.Start();         // Start one farmer thread
                Retailer chickenstore = new Retailer();
                ChickenFarm.priceCut += new
                                                      priceCutEvent(chickenstore.chickenOnSale);
                Thread[] retailers = new Thread[5];
                for (int i = 0; i < 2; i++)
                {   // Start N retailer threads
                    retailers[i] = new Thread(new
                                                       ThreadStart(chickenstore.retailerFunc));
                    retailers[i].Name = (i + 1).ToString();
                    retailers[i].Start();
                }
            }
        }
    }
}


